/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jdesktop.wonderland.modules.kinectcontrol.client.threads;

import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.wonderland.client.jme.JmeClientMain;

/**
 * Class for letting a avatar move forward in open wonderland Running thread
 * just simulates a key press of W through Robot (awt Framework)
 *
 *
 * @author Dominik
 */
public class KeyPress extends Thread {

    /**
     * Indicates if a thread is running
     */
    private boolean running;
    private Integer[] key;
    private int runtime;
    private static final Logger LOGGER =
            Logger.getLogger(JmeClientMain.class.getName());

    /**
     * Constructor for creating a Thread for pressing the key
     *
     * @param key Integer value of the key (awt.KeyEvent)
     * @param running Boolean to tell if thread is still acitve
     */
    public KeyPress(Integer[] key, boolean running, int runtime) {
        this.key = key;
        this.running = running;
        this.runtime = runtime;
    }

    /**
     * Run the walk forward move in open wonderland
     */
    @Override
    public void run() {
        setRunning(true);

        Robot keyPress;
        int counter = 0;

        try {

            //TODO Change to jMonkeyEngine key action
            keyPress = new Robot();
            boolean isMouse = getKey()[0].intValue() == 1;
            boolean isWheel = false;

            for (int i = 1; i < key.length - 1; i++) {
                if (getKey()[i].intValue() != 0) {
                    
                    LOGGER.log(Level.FINE, "Wheel: " + isWheel + " Mouse: " + isMouse + " Key: " + getKey()[i].intValue());
                    
                    if (isMouse && getKey()[i].intValue() != 507) {
                        if (isWheel) {
                            int anzPress = Math.abs(getKey()[i].intValue());
                            boolean negative = getKey()[i].intValue() < 0;
                            
                            for(int count=0;count<anzPress;count++)
                            {
                                if(negative)
                                    keyPress.mouseWheel(-1);
                                else
                                    keyPress.mouseWheel(1);
                                Thread.sleep(50);
                            }
                        } else {
                            keyPress.mousePress(InputEvent.getMaskForButton(getKey()[i].intValue()));
                        }

                    } else if (isMouse && getKey()[i].intValue() == 507) {
                        isWheel = true;
                    } else {
                        keyPress.keyPress(getKey()[i].intValue());
                    }
                }
            }

            sleep(runtime);

            for (int i = 1; i < key.length - 1; i++) {
                if (getKey()[i].intValue() != 0) {

                    if (getKey()[i].intValue() == 507) {
                        i = key.length;
                    } else if (isMouse) {
                        keyPress.mouseRelease(InputEvent.getMaskForButton(getKey()[i].intValue()));
                    } else {
                        keyPress.keyRelease(getKey()[i].intValue());
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        setRunning(false);
    }

    /**
     * @return the running
     */
    public boolean isRunning() {
        return running;
    }

    /**
     * @param running the running to set
     */
    public void setRunning(boolean running) {
        this.running = running;
    }

    /**
     * @return the key
     */
    public Integer[] getKey() {
        return key;
    }

    /**
     * @param key the key to set
     */
    public void setKey(Integer key[]) {
        this.key = key;
    }

    /**
     * @return the runtime
     */
    public int getRuntime() {
        return runtime;
    }

    /**
     * @param runtime the runtime to set
     */
    public void setRuntime(int runtime) {
        this.runtime = runtime;
    }
}
